package com.kurumi.medical.datasource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Order(-1)// 保证该AOP在@Transactional之前执行
@Component
public class DynamicDataSourceAspect {
	 private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceAspect.class);

	    @Before("@annotation(ds)")
	    public void changeDataSource(JoinPoint point, TargetDataSource ds) throws Throwable {
	        String dsId = ds.name();
	        System.out.println("---ds的名称---" + dsId);
	        if (!DynamicDataSourceContextHolder.containsDataSource(dsId)) {
	            logger.error("数据源[{}]不存在，使用默认数据源 > {}", ds.name(), point.getSignature());
	        } else {
	            logger.debug("Use DataSource : {} > {}", ds.name(), point.getSignature());
	            DynamicDataSourceContextHolder.setDataSourceType(ds.name());
	        }
	    }

	    @After("@annotation(ds)")
	    public void restoreDataSource(JoinPoint point, TargetDataSource ds) {
	        logger.debug("Revert DataSource : {} > {}", ds.name(), point.getSignature());
	        DynamicDataSourceContextHolder.clearDataSourceType();
	    }
	    
	    
}



